package cmj.cloudmusic.activity;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
import java.io.IOException;

import cmj.cloudmusic.R;

public class MediaPlayerActivity extends AppCompatActivity implements View.OnClickListener{

    private MediaPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media_player2);
        findViewById(R.id.local).setOnClickListener(this);
        findViewById(R.id.net).setOnClickListener(this);
        findViewById(R.id.pause).setOnClickListener(this);
        findViewById(R.id.begin).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
//        if(player!=null) {
//            //通知播放
//            player.stop();
//            //释放媒体资源
//            player.release();
////            //重置
////            player.reset();
//            //置空
//            player=null;
//        }
        if(v.getId()==R.id.local) {
            playLocal();
        } else if(v.getId()==R.id.net){
            playNet();
        } else if(v.getId()==R.id.pause){
            player.pause();
        } else {
            player.start();
        }
    }

    /**
     * 网络播放
     */
    private void playNet() {
        player=new MediaPlayer();
        player.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Toast.makeText(MediaPlayerActivity.this, "播放出现未知错误", Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        Uri uri=Uri.parse("http://yinyueshiting.baidu.com/data2/music/238976199/238976199.mp3?xcode=e33e916dd8159c6523407a79428c3f0f");
        try {
            player.setDataSource(this,uri);
            player.prepareAsync();
            player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    //开始播放
                    mp.start();
                }
            });
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    //停止播放
                    player.stop();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 本地播放
     */
    private void playLocal() {
        player=MediaPlayer.create(this,R.raw.notalking);
        player.start();
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                player.stop();
                Toast.makeText(MediaPlayerActivity.this, "播放完毕", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onDestroy() {
        if(player!=null) {
            player.stop();
            //释放流媒体资源
            player.release();
//            //回到初始状态
//            player.reset();
            player=null;
        }
        super.onDestroy();
    }
}
